package com.yin.myproject.thread;

import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReadWriteLock;
import java.util.concurrent.locks.ReentrantLock;

/**
 * @author Eason
 * @create 2018-03-14 10:10
 **/
public class SimpleReadWriteLock implements ReadWriteLock {

    private final Lock lock;
    private final Condition existReadCondition;
    private final Condition existWriteCondition;
    private final Lock readLock;
    private final Lock writeLock;
    private volatile boolean write;
    private volatile int readCount;

    public SimpleReadWriteLock() {
        lock = new ReentrantLock();
        readLock = new ReadLock();
        writeLock = new WriteLock();
        existReadCondition = lock.newCondition();
        existWriteCondition = lock.newCondition();
        write = false;
        readCount = 0;
    }

    @Override
    public Lock readLock() {
        return readLock;
    }

    @Override
    public Lock writeLock() {
        return writeLock;
    }

    private class ReadLock implements Lock {

        @Override
        public void lock() {
            lock.lock();
            try {
                while (write) {
                    try {
                        existReadCondition.await();
                    } catch (InterruptedException e) {
                        throw new RuntimeException("Interrupted");
                    }
                    readCount++;
                }
            } finally {
                lock.unlock();
            }
        }

        @Override
        public void lockInterruptibly() throws InterruptedException {

        }

        @Override
        public boolean tryLock() {
            return false;
        }

        @Override
        public boolean tryLock(long time, TimeUnit unit) throws InterruptedException {
            return false;
        }

        @Override
        public void unlock() {
            lock.lock();
            try {
                readCount--;
                if (readCount == 0) {
                    existReadCondition.signalAll();
                }
            } finally {
                lock.unlock();
            }
        }

        @Override
        public Condition newCondition() {
            return null;
        }
    }

    private class WriteLock implements Lock {

        @Override
        public void lock() {
            lock.lock();
            try {
                while (readCount > 0) {
                    try {
                        existReadCondition.await();
                    } catch (InterruptedException e) {
                        throw new RuntimeException("Interrupted");
                    }
                }
                while (write) {
                    try {
                        existWriteCondition.await();
                    } catch (InterruptedException e) {
                        throw new RuntimeException("Interrupted");
                    }
                }
                write = true;
            } finally {
                lock.unlock();
            }
        }

        @Override
        public void lockInterruptibly() throws InterruptedException {

        }

        @Override
        public boolean tryLock() {
            return false;
        }

        @Override
        public boolean tryLock(long time, TimeUnit unit) throws InterruptedException {
            return false;
        }

        @Override
        public void unlock() {
            lock.lock();
            try {
                write = false;
                existWriteCondition.signalAll();
            } finally {
                lock.unlock();
            }
        }

        @Override
        public Condition newCondition() {
            return null;
        }
    }
}
